/**
 * GTHttpRequestParameterScannerTest.h
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License 
 * at http://www.mozilla.org/MPL/
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 * either express or implied. See the License for the specific language governing rights and limitations 
 * under the License.
 *
 * The Original Code is gcast-toolbox4iphone.
 *
 * The Initial Developers of the Original Code is Giannandrea Castaldi.
 */
#import <SenTestingKit/SenTestingKit.h>
#import <UIKit/UIKit.h>
#import "GTHttpRequestScanner.h"
#import "GTHttpParameter.h"

@interface GTHttpRequestScannerTest : SenTestCase {
  NSDictionary *notifiedParameters;
}
@property(nonatomic,retain,readwrite) NSDictionary *notifiedParameters;
@end

@implementation GTHttpRequestScannerTest
@synthesize notifiedParameters;

- (void)setUp {
  self.notifiedParameters = nil;
}

- (void)tearDown {
  self.notifiedParameters = nil;
}

- (void)testNoParameterDetected {
  GTHttpRequestScanner *scanner = [GTHttpRequestScanner scanner];
  [scanner detectParameter:@"name"];
  STAssertEqualObjects(
                       [scanner scanRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]],
                       [NSDictionary dictionary],
                       @"It shouldn't find any parameter");
}

- (void)testSimpleParameterDetected {
  GTHttpRequestScanner *scanner = [GTHttpRequestScanner scanner];
  [scanner detectParameter:@"name"];
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
  [request setHTTPBody:[@"name=john" dataUsingEncoding:NSASCIIStringEncoding]];
  STAssertEqualObjects(
                       [scanner scanRequest:request],
                       [NSDictionary dictionaryWithObject:[GTHttpParameter parameterWithName:@"name" value:@"john"] forKey:@"name"],
                       @"It should find the parameter");
}

- (void)testParameterWithoutValueDetected {
  GTHttpRequestScanner *scanner = [GTHttpRequestScanner scanner];
  [scanner detectParameter:@"name"];
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
  [request setHTTPBody:[@"name" dataUsingEncoding:NSASCIIStringEncoding]];
  STAssertEqualObjects(
                       [scanner scanRequest:request],
                       [NSDictionary dictionaryWithObject:[GTHttpParameter parameterWithName:@"name" value:nil] forKey:@"name"],
                       @"It should find the parameter");
  [request setHTTPBody:[@"name=" dataUsingEncoding:NSASCIIStringEncoding]];
  STAssertEqualObjects(
                       [scanner scanRequest:request],
                       [NSDictionary dictionaryWithObject:[GTHttpParameter parameterWithName:@"name" value:nil] forKey:@"name"],
                       @"It should find the parameter");
}

- (void)testParameterDecoding {
  GTHttpRequestScanner *scanner = [GTHttpRequestScanner scanner];
  [scanner detectParameter:@"email"];
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
  [request setHTTPBody:[@"email=bravofly.tester%40gmail.com" dataUsingEncoding:NSASCIIStringEncoding]];
  STAssertEqualObjects(
                       [scanner scanRequest:request],
                       [NSDictionary dictionaryWithObject:[GTHttpParameter parameterWithName:@"email" value:@"bravofly.tester@gmail.com"] forKey:@"email"],
                       @"It should find the parameter");
}

- (void)testNotificationNotSentIfNoParameterFound {
  GTHttpRequestScanner *scanner = [GTHttpRequestScanner scanner];
  [scanner detectParameter:@"name"];
  [scanner notifyDetectedParametersTarget:self action:@selector(setNotifiedParameters:)];
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
  [scanner scanRequest:request];
  STAssertNil(self.notifiedParameters, @"It shouldn't notify the parameter");
}

- (void)testNotificationSentIfNoParameterFound {
  GTHttpRequestScanner *scanner = [GTHttpRequestScanner scanner];
  [scanner detectParameter:@"name"];
  [scanner notifyDetectedParametersTarget:self action:@selector(setNotifiedParameters:)];
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
  [request setHTTPBody:[@"name=john" dataUsingEncoding:NSASCIIStringEncoding]];
  [scanner scanRequest:request];
  STAssertEqualObjects(
                       self.notifiedParameters,
                       [NSDictionary dictionaryWithObject:[GTHttpParameter parameterWithName:@"name" value:@"john"] forKey:@"name"],
                       @"It should notify the parameter");
}

- (void)testNSScanner {
  NSScanner *scanner = [NSScanner scannerWithString:@"name1=value1&name2=value2"];
  [scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"&"]];
  NSString *found = nil;
  while ([scanner scanUpToString:@"&" intoString:&found] || [scanner scanUpToString:@"" intoString:&found]) {
//    NSLog(@"found: %@", found);
  }
}

@end
